const Request = require('./libs/request')
const faker = require('faker/locale/zh_CN')
const knex = require('./libs/knex')
const Redis = require('ioredis')
class Api {
    constructor() {
        this.$knex = knex
        this.$http = new Request()
        this.$redis = new Redis(6379, '127.0.0.1')      
        this.uri = 'http://sql.faker.dev/test'
        this.product_add(100)
        return this
    }
    async test () {
       
        let data = {
            "app_key": 'large mini',
            "sign": 'small test key',
            "method": "product.add",
            "language": 'small test key',
            "timestamp": 'small test key',
            "description": "添商品测试55",
            "weight": "1.2",
            "price": "18.5",
            "origin_country": "CN"
        }
        let res =  await this.$http.get(this.uri, data)
        console.log(res)

    }
    async product_add (limit=10) {
        let id = 1
        limit = id + parseInt(limit);
        let origin_country = ['US', 'CN', 'JP', 'KR']
        while(id < limit) {
            let data = {
                'partner_id': 8,
                'description': faker.name.findName(),
                'price': faker.random.number({
                    min:10,
                    max:100,
                    precision: 0.01
                }),
                'weight': faker.random.number({
                    min:1.2,
                    max:10.0,
                    precision: 0.01
                }),
                'origin_country': faker.random.arrayElement(origin_country),
                'created': Math.round(new Date().getTime() /1000)
            }
            await this.$redis.hset('products', id, JSON.stringify(data))
            let res = await this.$http.post(this.uri, data)
            console.log(res)
            id++
        }
        // 请求接口
    }
    async box_add_packages (limit=10) {
        let id = 1
        limit = id + parseInt(limit);
        let origin_country = ['US', 'CN', 'JP', 'KR']
        while(id < limit) {
            let data = {
                'partner_id': 8,
                'description': faker.name.findName(),
                'price': faker.random.number({
                    min:10,
                    max:100,
                    precision: 0.01
                }),
                'weight': faker.random.number({
                    min:1.2,
                    max:10.0,
                    precision: 0.01
                }),
                'origin_country': faker.random.arrayElement(origin_country),
                'created': Math.round(new Date().getTime() /1000)
            }
            let res = await this.$http.post(this.uri, data)
            console.log(res)
            id++
        }
        // 请求接口
    }
    // 包裹添加商品
    async packages_products() {
        console.log(123)
    }
}

const A = new Api()
